package com.chenghao.camerapic;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class MainActivity extends Activity implements View.OnClickListener{

    private ImageView myimageview;
    private Button but_pic;
    private Button but_cam;
    private  String IMAGE_UNSPECIFIED = "image/*";

    private  String HEADIMAGE = Environment.getExternalStorageDirectory() + "/CHPIC";


    // 取消
    private static final int NONE = 0;
    // 拍照
    private static final int PHOTOHRAPH = 1;
    // 选择相片
    private int PHOTOZOOM = 2;
    // OK
    private static final int OK = 3;

    private String TAG="chenghao";

    private File imgfile;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //先创建文件夹
        File file = new File(HEADIMAGE);
        if (!file.exists()) {
            file.mkdirs();
        }


        myimageview= (ImageView) findViewById(R.id.myimageview);
        but_pic= (Button) findViewById(R.id.but_pic);
        but_cam= (Button) findViewById(R.id.but_cam);
        but_pic.setOnClickListener(this);
        but_cam.setOnClickListener(this);
        imgfile=new File(HEADIMAGE, "okheadImage.jpg");


    }
    public void showlog(String str){
        Log.i(TAG,"打印内容＝"+str);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.but_pic:
                Intent intent01 = new Intent(Intent.ACTION_GET_CONTENT, null);
                intent01.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                        IMAGE_UNSPECIFIED);
                startActivityForResult(intent01, PHOTOZOOM);
                break;

            case R.id.but_cam:
                Toast.makeText(MainActivity.this,"没有实现功能",Toast.LENGTH_LONG).show();
                break;
        }
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        showlog("返回的requestCode＝"+requestCode);

        if (requestCode == NONE)
            return;
        // 拍照
        if (requestCode == PHOTOHRAPH) {
            showlog("1，到拍照这,先进行裁剪功能");
            String FILE_PATH=HEADIMAGE +"/headImage.jpg";
            Intent intent = new Intent();
            intent.setAction("com.android.camera.action.CROP");
            intent.setDataAndType(Uri.fromFile(new File(HEADIMAGE, "headImage.jpg")), "image/*");// mUri是已经选择的图片Uri
            intent.putExtra("crop", "true");
            intent.putExtra("aspectX", 1);// 裁剪框比例
            intent.putExtra("aspectY", 1);
            intent.putExtra("outputX", 150);// 输出图片大小
            intent.putExtra("outputY", 150);
            intent.putExtra("return-data", true);
            startActivityForResult(intent, OK);
        }
        //选择相片
        else if(requestCode==PHOTOZOOM){
            if(data==null){
                return;
            }
            Uri  url= data.getData();//获取选择图片的URI
            showlog("1，到选取照片这,先进行裁剪功能");
            Intent intent = new Intent();
            intent.setAction("com.android.camera.action.CROP");
            intent.setDataAndType(url, "image/*");// mUri是已经选择的图片Uri
            intent.putExtra("crop", "true");
            intent.putExtra("aspectX", 1);// 裁剪框比例
            intent.putExtra("aspectY", 1);
            intent.putExtra("outputX", 150);// 输出图片大小
            intent.putExtra("outputY", 150);
            intent.putExtra("return-data", true);
            startActivityForResult(intent, OK);
        }
        else if(requestCode==OK){
            showlog("2，走到裁剪完成图片后，开始压缩图片。");
            // 拿到剪切数据
            Bitmap bmap = data.getParcelableExtra("data");
            if(bmap==null){
                return;
            }
            showlog("bmap="+bmap);
            // 图像保存到文件中
            FileOutputStream foutput = null;
            try {
                foutput = new FileOutputStream(imgfile);
                //第二个参数影响的是图片的质量，但是图片的宽度与高度是不会受影响滴,30 是压缩率，表示压缩70%; 如果不压缩是100，表示压缩率为0
                bmap.compress(Bitmap.CompressFormat.PNG, 30, foutput);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }finally{
                if(null != foutput){
                    try {
                        foutput.close();
                        showlog("完成");
                        showlog("3，保存图片成功，开始压缩图片。");

                        Bitmap bim=BitmapFactory.decodeFile(HEADIMAGE+"/okheadImage.jpg");
                        myimageview.setImageBitmap(bim);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }


}
